<?php
// ----------------------------------------------------------------------
// pn-commerce Shopping Cart Solution for PostNuke
// http://pncommerce.de
// http://www.sourceforge.net/projects/pn-commerce/
//
// It is based on PostKart 0.701QA from
//			Destin LeBlanc [ postkart@zenflesh.com ]
// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/pnprstringapi.php,v $
//
// Last changes from:  $Author: landseer $
//
// Revision:           $Revision: 1.15 $
//
// Date of revision    $Date: 2005/03/23 12:09:44 $
//
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------

require_once("modules/pncommerce/common.php");

/**
 * This is just to show the name of this api in the scheme edit area
 * This is a necessary function, don't remove it!
 *
 * @return string
 */
function pncommerce_prstringapi_info ($args)
{
  return _PNC_STRINGAPI;
}

/**
 * This function is called during the getItem procedure
 * It sets a field of $args[object] with the name $args['object']['property']['name'] to the value of the stringfield field of the property field
 * This is a necessary function, don't remove it!
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prstringapi_load ($args)
{
  if(!isset($args['object'][$args['property']['name']]))$args['object']['dynamicfields'][]=$args['property']['name'];
  $args['object'][$args['property']['name']]=$args['property']['stringfield'];
  return $args['object'];
}


/**
 * This function returns the stuff from the property array for the given purpose
 * The api can decide which template to use. Even formparts are possible
 * see the processForm for details about this
 * @param $args[purpose] string the porpose for example itemview, cartview or something like that
 * @param $args[purposedata] array special data for this purpose, for example the old and new amount of the item in the cart or something like that
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prstringapi_getVisualOutput ($args)
{
  switch($args[purpose])
  {
    case "showtemplate":
       $template=$args['purposedata'];
       break;
    case "objectedit":
       $template="objectedit";
       break;
    case "schemeedit":
       $template="schemeedit";
       break;
//This is a senseless example
    case "cartview":
    case "invoiceview":
    default:
       $template="objectview";
  }
  $smarty =& new pnRender('pncommerce');
  $smarty->caching = false;
  $smarty->assign( 'params', $args );
  return $smarty->fetch( "pncommerce_prstringapi_".$template.".html" );
}


/**
 * This function processes a form if necessary (for dropdown fields for sizes etc.)
 * Data should be processed and the modified objectarray has to be returned.
 * all data that should be saved permanently has to be stored in the apropriate propertiesdata field
 * SchemeEdit has it's own process function!
 * -
 * @param $args[purpose] string the porpose for example itemview, cartview or something like that
 * @param $args[purpose data] array special data for this purpose, for example the old and new amount of the item in the cart or something like that
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prstringapi_processForm ($args)
{

//This is a senseless example
  switch($args['purpose'])
  {
    case "objectedit":
       $args['object']['PropertiesData'][$args['property']['propertydata_id']]['stringfield']=pnVarCleanFromInput("prstringapi_".$args['property']['propertydata_id']."_".$args['property']['object_id']);
       $args['object'][$args['property']['name']]=$args['property']['stringfield'];
    default:
    //<sleep>zzZzZzz</sleep>
  }
  return $args['object'];
}

/**
 * This function processes the scheme form and saves the data to the DB
 * @param $args[oldproperty] the old property
 */
function pncommerce_prstringapi_processSchemeForm ($args)
{
    if (!pnModAPILoad('pncommerce', 'admin'))
    {
        return showerrorpage("unable to load pnadminapi", __FILE__, __LINE__);

    }
//We should check for changes here and save only if necessary
//can be improved later :-)
    if(pnVarCleanFromInput($args['oldproperty']['property_id']."_prstringapi_delete") <> '1') {
        $prop=array('property_id'=>$args['oldproperty']['property_id'],
                  'scheme_id'=>$args['oldproperty']['scheme_id'],
                  'type'=>"prstring");
        $prop['name']=pnVarCleanFromInput($args['oldproperty']['property_id']."_prstringapi_name");

        $config=array();
        $config['defaultvalue']=pnVarCleanFromInput($args['oldproperty']['property_id']."_prstringapi_defaultvalue");
        $config['hideifempty']=(pnVarCleanFromInput($args['oldproperty']['property_id']."_prstringapi_hideifempty")==1) ? 1: 0;
        $prop['config']=$config;

        $hidden = pnVarCleanFromInput($args['oldproperty']['property_id']."_prstringapi_hidden");
        $prop['hidden'] = ($hidden==1) ? 1 : 0;
        //$prop['sortorder']=pnVarCleanFromInput($args['oldproperty']['property_id']."_prstringapi_sortorder");
        return pnModAPIFunc( 'pncommerce', 'admin', 'propertyStore', $prop);
    } else {
        return pnModAPIFunc( 'pncommerce', 'admin', 'propertyDelete', array('property_id' => $args['oldproperty']['property_id']));
    }
}

/**
 * This function returns a new property with defaultdata
 * @return array
 */
function pncommerce_prstringapi_new ($args)
{
    $prop=array('property_id'=>-1,
              'scheme_id'=>-1,
              'type'=>"prstring");
    $prop['name']="prString";
    $prop['config']="";//pnVarCleanFromInput($args['property_id']."_prstringapi_string");
    $prop['hidden']=0;
    $prop['sortorder'] = (int)$args['sortorder'];
    return $prop;
}

/**
 * This function returns a new propertydata block with defaultdata
 * @param $args['property'] array property
 * @return array
 */
function pncommerce_prstringapi_newdata ($args)
{
    extract($args);
    unset($args);

    $property['stringfield'] = $property['config']['defaultvalue'] ;//default value would be good here
    return $property;
}



?>
